package com.sh.springbootinit.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.sh.springbootinit.model.entity.movie.Movie;
import com.sh.springbootinit.model.vo.MovieVo;
import com.sh.springbootinit.service.MovieService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: sh
 * @Date: 2025/1/11 18:00
 * @Description:
 */
@Service
public class MovieServiceImpl implements MovieService {

    @Override
    public List<Movie> searchMovie(String searchText) throws IOException {
        List<Movie> res = new ArrayList<>();
        // 这里实现搜索电影的逻辑   如果有searchText传过来  就搜索准确的
        List<Movie> movies = getMovies();
        if (searchText != null) {
            // 筛选出包含searchText的电影
            for (int i = 0; i < movies.size(); i++) {
                String totalStr = "";
                totalStr += movies.get(i).getName();
                totalStr += movies.get(i).getCategories().toString();
                totalStr += movies.get(i).getSimpleDetail();
                totalStr += movies.get(i).getReleaseTime();
                if (totalStr.contains(searchText)) {
                    res.add(movies.get(i));
                }
            }
            return res;
        } else {
            return movies;
        }
    }

    private static List<Movie> getMovies() throws IOException {
        String baseUrl = "https://ssr1.scrape.center";
        Document baseDocument = Jsoup.connect(baseUrl).get();
        Elements temp = baseDocument.getElementsByClass("el-pagination__total");
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(temp.text());
        // 总共多少条
        Integer number = matcher.find() ? Integer.valueOf(matcher.group()) : 1;
        Elements select = baseDocument.select(".el-card__body");
        number = number / select.size();
        List<Movie> movies = new ArrayList<>();
        // 抓取全部
        for (Integer i = 1; i <= number; i++) {
            String pageUrl = "https://ssr1.scrape.center/page/" + i;
            Document document = Jsoup.connect(pageUrl).get();
            Elements movieCards = document.select(".el-card__body .el-row");
            for (Element movieCard : movieCards) {
                Movie movie = new Movie();

                Elements firstDivs = movieCard.select("> div");
                Element firstDiv = firstDivs.get(0);  // 拿详情地址和图片
                Elements aTag = firstDiv.select("a");
                String detailUrl = "https://ssr1.scrape.center" + aTag.attr("href");
                Elements imgTag = firstDiv.select("a img");
                String imageUrl = imgTag.attr("src");
                movie.setDetailUrl(detailUrl);
                movie.setImageUrl(imageUrl);

                Element secondDiv = firstDivs.get(1);  // 拿详情介绍和标签
                Elements nameElement = secondDiv.select("h2");
                String name = nameElement.text();
                Elements categories = secondDiv.select(".categories span");
                List<String> categorieList = new ArrayList<>();
                for (Element category : categories) {
                    String span = category == null ? "" : category.text();
                    categorieList.add(span);
                }
                movie.setName(name);
                movie.setCategories(categorieList);
                Elements infos = secondDiv.select(".m-v-sm.info");
                Element simpleDetailElement = infos.get(0);
                Elements simpleDetailElementSpans = simpleDetailElement.select("span");
                String simpleDetail = "";
                for (Element simpleDetailElementSpan : simpleDetailElementSpans) {
                    String text = simpleDetailElementSpan == null ? "" : simpleDetailElementSpan.text();
                    simpleDetail += text;
                }
                movie.setSimpleDetail(simpleDetail);

                Element releaseTimeElement = infos.get(1);
                Element releaseTimeSpan = releaseTimeElement.selectFirst("span");
                String releaseTime = releaseTimeSpan == null ? "" : releaseTimeSpan.text();
                movie.setReleaseTime(releaseTime);

                Element threeDiv = firstDivs.get(2);   // 拿评分
                Element rateElement = threeDiv.selectFirst(".score.m-t-md.m-b-n-sm");
                String rating = rateElement == null ? "-" : rateElement.text();
                movie.setRating(Double.valueOf(rating));
                movies.add(movie);
            }
        }
        return movies;
    }
}

